package vista;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class MainApp extends Application {
	
	private Stage primaryStage;
	private static BorderPane rootLayout;
	private CtrRootLayout controlleroot;
	private String usuario=null;
	
	public MainApp() {
		
		}
	
	@Override
	public void start(Stage primaryStage) {
		this.primaryStage = primaryStage;
		this.primaryStage.setTitle("Farmacia de Dios");
		
		try {
			// se carga la interfaz principal 
			FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("RootLayout.fxml"));
			rootLayout = (BorderPane) loader.load();
			Scene scene = new Scene(rootLayout);
			primaryStage.setScene(scene);
			primaryStage.setFullScreen(true);
			primaryStage.show();
			
			controlleroot = loader.getController();
			controlleroot.setMainApp(this);
		} catch (IOException e) {
			// Exception si el archivo fxml no se pude cargar
			e.printStackTrace();
		}
		
		showLoginDialog();
	}
		
	/**
	 * Returns the main stage.
	 * @return
	 */
	public Stage getPrimaryStage() {
		return primaryStage;
	}
	
	/**
	 * muestra la interfaz de login
	 */
	

	public void showfrmClientes() {
		try {
			// Load the fxml file and set into the center of the main layout
			FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("FrmClientes.fxml"));
			AnchorPane overviewPage = (AnchorPane) loader.load();
			rootLayout.setCenter(overviewPage);
			
			// Give the controller access to the main app
			ClCtrClientes controller = loader.getController();
			controller.setMainApp(this);
			
		} catch (IOException e) {
			// Exception gets thrown if the fxml file could not be loaded
			e.printStackTrace();
		}
	}
	
	
	public void showfrmUsuarios() {
		try {
			// Load the fxml file and set into the center of the main layout
			FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("frmUsuarios.fxml"));
			AnchorPane overviewPage = (AnchorPane) loader.load();
			rootLayout.setCenter(overviewPage);
			
			// Give the controller access to the main app
			ClCtrUsuarios controller = loader.getController();
			controller.setMainApp(this);
			
		} catch (IOException e) {
			// Exception gets thrown if the fxml file could not be loaded
			e.printStackTrace();
		}
	}
	
	public void showfrmCajas() {
		try {
			// Load the fxml file and set into the center of the main layout
			FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("frmCajas.fxml"));
			AnchorPane overviewPage = (AnchorPane) loader.load();
			rootLayout.setCenter(overviewPage);
			
			// Give the controller access to the main app
			ControladorCajas controller = loader.getController();
			controller.setMainApp(this);
			
		} catch (IOException e) {
			// Exception gets thrown if the fxml file could not be loaded
			e.printStackTrace();
		}
	}
	
	public void showfrmProveedores() {
		try {
			// Load the fxml file and set into the center of the main layout
			FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("frmProveedores.fxml"));
			AnchorPane overviewPage = (AnchorPane) loader.load();
			rootLayout.setCenter(overviewPage);
			
			// Give the controller access to the main app
			ControladorProveedor controller = loader.getController();
			controller.setMainApp(this);
			
		} catch (IOException e) {
			// Exception gets thrown if the fxml file could not be loaded
			e.printStackTrace();
		}
	}
	


	public void showLoginDialog() {
		try {
			// se carga el archivo fxml y crea el nuevo stage para la ventana  
			FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("FrmLogin.fxml"));
			BorderPane page = (BorderPane) loader.load();
			Stage dialogStage = new Stage();
			dialogStage.setTitle("Login");
			dialogStage.initModality(Modality.WINDOW_MODAL);
			dialogStage.initOwner(primaryStage);
			Scene scene = new Scene(page);
			dialogStage.setScene(scene);
			
			// da acceso a MainApp a la clase controlador de login
			ControladorLogin controller = loader.getController();
			controller.setDialogStage(dialogStage);
			
			
			// oculta la ventana principal
			primaryStage.hide();
			//visualiza la ventana del login
			dialogStage.showAndWait();
			usuario=controller.getUsuario();
			if(usuario!=null){
				controlleroot.setUsuario(usuario);
				primaryStage.show();
			}
			else{
				primaryStage.close();
			}
			//una vez que se cierra la ventana del login se muestra la ventana principal
							
			
		} catch (IOException e) {
			// Exception si el archivo fxml no se puede cargar
			e.printStackTrace();	
		
		}
	}
	
	
	
	
	public void showLogin() {
		try {
			// se carga el archivo fxml y crea el nuevo stage para la ventana  
			FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("FrmLogin.fxml"));
			BorderPane page = (BorderPane) loader.load();
			Stage dialogStage = new Stage();
			dialogStage.setTitle("Login");
			dialogStage.initModality(Modality.WINDOW_MODAL);
			dialogStage.initOwner(primaryStage);
			Scene scene = new Scene(page);
			dialogStage.setScene(scene);
			
			// da acceso a MainApp a la clase controlador de login
			ControladorLogin controller = loader.getController();
			controller.setDialogStage(dialogStage);
			
			
			// oculta la ventana principal
			primaryStage.hide();
			//visualiza la ventana del login
			dialogStage.showAndWait();
			usuario=controller.getUsuario();
			if(usuario!=null){
				controlleroot.setUsuario(usuario);
				primaryStage.show();
			}
			else{
				primaryStage.close();
			}
			//una vez que se cierra la ventana del login se muestra la ventana principal
							
			
		} catch (IOException e) {
			// Exception si el archivo fxml no se puede cargar
			e.printStackTrace();	
		
		}
	}
	
	
	
	
	
	

	/*
	public Diagnosticos showDiagnosticosDialog() {
		try {
			// Load the fxml file and create a new stage for the popup
			FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("frmListaDiagnosticos.fxml"));
			AnchorPane page = (AnchorPane) loader.load();
			Stage dialogStage = new Stage();
			dialogStage.setTitle("Lista de diagn�sticos");
			dialogStage.initModality(Modality.WINDOW_MODAL);
			dialogStage.initOwner(primaryStage);
			Scene scene = new Scene(page);
			dialogStage.setScene(scene);
			
			// Set the person into the controller
			controlador.CtrListaDiagnosticos controller = loader.getController();
			controller.setDialogStage(dialogStage);
			//paciente=controller.getPaciente();
			
			// Show the dialog and wait until the user closes it
			dialogStage.showAndWait();
			return controller.getDiagnostico();
			
		
			
		} catch (IOException e) {
			// Exception gets thrown if the fxml file could not be loaded
			e.printStackTrace();
			return null;
		
		}
	}

	*/
	
	
		public static void main(String[] args) {
		launch(args);
	}
		
		
		
		
		
		
		
		
		public static void showfrmProductos() {
			// TODO Auto-generated method stub
			try {
				// Load the fxml file and set into the center of the main layout
				FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("FrmProductos.fxml"));
				AnchorPane overviewPage = (AnchorPane) loader.load();
				rootLayout.setCenter(overviewPage);
				
				// Give the controller access to the main app
				ClCtrProductos controller = loader.getController();
				controller.setMainApp(null);
				
			} catch (IOException e) {
				// Exception gets thrown if the fxml file could not be loaded
				e.printStackTrace();
			}
			
		}
		
		public static void showfrmGenerico() {
			// TODO Auto-generated method stub
			try {
				// Load the fxml file and set into the center of the main layout
				FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("FrmGenerico.fxml"));
				AnchorPane overviewPage = (AnchorPane) loader.load();
				rootLayout.setCenter(overviewPage);
				
				// Give the controller access to the main app
				ClCtrGenerico controller = loader.getController();
				controller.setMainApp(null);
				
			} catch (IOException e) {
				// Exception gets thrown if the fxml file could not be loaded
				e.printStackTrace();
			}
			
		}
		
		
		
		public static void showfrmPatente() {
			// TODO Auto-generated method stub
			try {
				// Load the fxml file and set into the center of the main layout
				FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("FrmPatente.fxml"));
				AnchorPane overviewPage = (AnchorPane) loader.load();
				rootLayout.setCenter(overviewPage);
				
				// Give the controller access to the main app
				ClCtrPatente controller = loader.getController();
				controller.setMainApp(null);
				
			} catch (IOException e) {
				// Exception gets thrown if the fxml file could not be loaded
				e.printStackTrace();
			}
			
		}
		
		
		
		
}
